package com.lam.framework.bean.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.constant.CommonConstant;
import com.lam.common.validate.constant.ValidConstant;
import com.lam.common.validate.model.DataUsedDesc;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.service.ValidateService;
import com.lam.framework.bean.entity.SysTableUnique;
import com.lam.framework.bean.mapper.TableUniqueMapper;
import com.lam.framework.bean.mapper.ValidateMapper;

@Service
public class ValidateServiceImpl implements ValidateService {

	@Autowired
    private ValidateMapper validateMapper;
	@Autowired
	private TableUniqueMapper tableUniqueMapper;
    
    @Cacheable(value = ValidConstant.TABLE_UNIQUE_CONFIG + "#1D", key = "#root.args[0]", unless = "#result == null")
    @Override
    @Transactional(readOnly = true)
	public UniqueDesc selectUniqueByCode(String tableUniqueCode) {
    	QueryWrapper<SysTableUnique> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", tableUniqueCode);
        queryWrapper.eq("status", CommonConstant.STATUS_NORMAL_1);
        queryWrapper.last("limit 1");
        SysTableUnique tableUnique = tableUniqueMapper.selectOne(queryWrapper);
        
		return tableUnique == null ? null : new UniqueDesc(tableUniqueCode, tableUnique.getTableName(), 
				tableUnique.getUniqueField(), tableUnique.getQueryField(), tableUnique.getConditionSql());
	}


	@Override
	@Transactional(readOnly = true)
	public Map<String, Object> selectUniqueData(UniqueDesc uniqueDesc) {
		
		return validateMapper.selectUniqueData(uniqueDesc);
	}

	@Override
	@Transactional(readOnly = true)
	public List<String> dataExist(DataUsedDesc dataUsedDesc) {
		
		return validateMapper.dataExist(dataUsedDesc);
	}

}
